
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>数据库函数 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="分页器" href="../paginator.html" />
    <link rel="prev" title="条件表达式" href="conditional-expressions.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="conditional-expressions.html" title="条件表达式">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="../paginator.html" title="分页器">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-database-functions">
            
  <div class="section" id="s-module-django.db.models.functions">
<span id="s-database-functions"></span><span id="module-django.db.models.functions"></span><span id="database-functions"></span><h1>数据库函数<a class="headerlink" href="#module-django.db.models.functions" title="永久链接至标题">¶</a></h1>
<p>下面记录的类为用户提供了一种方法，可以在 Django 中使用底层数据库提供的函数作为注解、聚合或过滤器。函数也是 <a class="reference internal" href="expressions.html"><span class="doc">表达式</span></a>，所以它们可以和其他表达式一起使用和组合，比如 <a class="reference internal" href="querysets.html#aggregation-functions"><span class="std std-ref">聚合函数</span></a>。</p>
<p>我们将在每个函数的例子中使用以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">age</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">PositiveIntegerField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">alias</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">goes_by</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>我们通常不建议允许 <code class="docutils literal notranslate"><span class="pre">null=True</span></code> 为 <code class="docutils literal notranslate"><span class="pre">CharField</span></code>，因为这允许字段有两个 <code class="docutils literal notranslate"><span class="pre">Coalesce</span></code>，但它对下面的 <code class="docutils literal notranslate"><span class="pre">Coalesce</span></code> 例子很重要。</p>
<div class="section" id="s-comparison-and-conversion-functions">
<span id="s-comparison-functions"></span><span id="comparison-and-conversion-functions"></span><span id="comparison-functions"></span><h2>比较和转换函数<a class="headerlink" href="#comparison-and-conversion-functions" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-cast">
<span id="cast"></span><h3><code class="docutils literal notranslate"><span class="pre">Cast</span></code><a class="headerlink" href="#cast" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Cast">
<em class="property">class </em><code class="descname">Cast</code>(<em>expression</em>, <em>output_field</em>)<a class="headerlink" href="#django.db.models.functions.Cast" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>强制 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 的结果类型为 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 的类型。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cast</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">age</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">age_as_float</span><span class="o">=</span><span class="n">Cast</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">FloatField</span><span class="p">()),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">age_as_float</span><span class="p">)</span>
<span class="go">25.0</span>
</pre></div>
</div>
</div>
<div class="section" id="s-coalesce">
<span id="coalesce"></span><h3><code class="docutils literal notranslate"><span class="pre">Coalesce</span></code><a class="headerlink" href="#coalesce" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Coalesce">
<em class="property">class </em><code class="descname">Coalesce</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Coalesce" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受至少两个字段名或表达式的列表，并返回第一个非空值（注意，空字符串不被视为空值）。每个参数必须是同样的类型，因此混合文本和数字将导致数据库错误。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get a screen name from least to most public</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Sum</span><span class="p">,</span> <span class="n">Value</span> <span class="k">as</span> <span class="n">V</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Coalesce</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">,</span> <span class="n">goes_by</span><span class="o">=</span><span class="s1">&#39;Maggie&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">screen_name</span><span class="o">=</span><span class="n">Coalesce</span><span class="p">(</span><span class="s1">&#39;alias&#39;</span><span class="p">,</span> <span class="s1">&#39;goes_by&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">screen_name</span><span class="p">)</span>
<span class="go">Maggie</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Prevent an aggregate Sum() from returning None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">aggregated</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">combined_age</span><span class="o">=</span><span class="n">Coalesce</span><span class="p">(</span><span class="n">Sum</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">),</span> <span class="n">V</span><span class="p">(</span><span class="mi">0</span><span class="p">)),</span>
<span class="gp">... </span>   <span class="n">combined_age_default</span><span class="o">=</span><span class="n">Sum</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">aggregated</span><span class="p">[</span><span class="s1">&#39;combined_age&#39;</span><span class="p">])</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">aggregated</span><span class="p">[</span><span class="s1">&#39;combined_age_default&#39;</span><span class="p">])</span>
<span class="go">None</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>在 MySQL 上传递给 <code class="docutils literal notranslate"><span class="pre">Coalesce</span></code> 的 Python 值可能会被转换为不正确的类型，除非明确地转换为正确的数据库类型：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">DateTimeField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cast</span><span class="p">,</span> <span class="n">Coalesce</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">now</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Coalesce</span><span class="p">(</span><span class="s1">&#39;updated&#39;</span><span class="p">,</span> <span class="n">Cast</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="n">DateTimeField</span><span class="p">()))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-collate">
<span id="collate"></span><h3><code class="docutils literal notranslate"><span class="pre">Collate</span></code><a class="headerlink" href="#collate" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Collate">
<em class="property">class </em><code class="descname">Collate</code>(<em>expression</em>, <em>collation</em>)<a class="headerlink" href="#django.db.models.functions.Collate" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>Takes an expression and a collation name to query against.</p>
<p>For example, to filter case-insensitively in SQLite:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">Collate</span><span class="p">(</span><span class="n">Value</span><span class="p">(</span><span class="s1">&#39;john&#39;</span><span class="p">),</span> <span class="s1">&#39;nocase&#39;</span><span class="p">))</span>
<span class="go">&lt;QuerySet [&lt;Author: John&gt;, &lt;Author: john&gt;]&gt;</span>
</pre></div>
</div>
<p>It can also be used when ordering, for example with PostgreSQL:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">Collate</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;et-x-icu&#39;</span><span class="p">))</span>
<span class="go">&lt;QuerySet [&lt;Author: Ursula&gt;, &lt;Author: Veronika&gt;, &lt;Author: Ülle&gt;]&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-greatest">
<span id="greatest"></span><h3><code class="docutils literal notranslate"><span class="pre">Greatest</span></code><a class="headerlink" href="#greatest" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Greatest">
<em class="property">class </em><code class="descname">Greatest</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Greatest" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受至少两个字段名或表达式的列表，并返回最大的值。每个参数必须是同样的类型，所以混合文本和数字会导致数据库错误。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">modified</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Comment</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">modified</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">blog</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Blog</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>

<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Greatest</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">blog</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">body</span><span class="o">=</span><span class="s1">&#39;Greatest is the best.&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">comment</span> <span class="o">=</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">body</span><span class="o">=</span><span class="s1">&#39;No, Least is better.&#39;</span><span class="p">,</span> <span class="n">blog</span><span class="o">=</span><span class="n">blog</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">comments</span> <span class="o">=</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">last_updated</span><span class="o">=</span><span class="n">Greatest</span><span class="p">(</span><span class="s1">&#39;modified&#39;</span><span class="p">,</span> <span class="s1">&#39;blog__modified&#39;</span><span class="p">))</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">annotated_comment</span> <span class="o">=</span> <span class="n">comments</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">annotated_comment.last_updated</span></code> 将是 <code class="docutils literal notranslate"><span class="pre">blog.modified</span></code> 和 <code class="docutils literal notranslate"><span class="pre">comment.modified</span></code> 中最近的。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>当一个或多个表达式可能为 <code class="docutils literal notranslate"><span class="pre">null</span></code> 时，<code class="docutils literal notranslate"><span class="pre">Greatest</span></code> 的行为在不同的数据库之间有所不同。</p>
<ul class="simple">
<li>PostgreSQL：<code class="docutils literal notranslate"><span class="pre">Greatest</span></code> 将返回最大的非空表达式，如果所有表达式都是 <code class="docutils literal notranslate"><span class="pre">null</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">null</span></code>。</li>
<li>SQLite、Oracle 和 MySQL。如果任何表达式是 <code class="docutils literal notranslate"><span class="pre">null</span></code>，<code class="docutils literal notranslate"><span class="pre">Greatest</span></code> 将返回 <code class="docutils literal notranslate"><span class="pre">null</span></code>。</li>
</ul>
<p class="last">如果你知道一个合理的最小值作为默认值，可以使用 <code class="docutils literal notranslate"><span class="pre">Coalesce</span></code> 来模拟 PostgreSQL 的行为。</p>
</div>
</div>
<div class="section" id="s-jsonobject">
<span id="jsonobject"></span><h3><code class="docutils literal notranslate"><span class="pre">JSONObject</span></code><a class="headerlink" href="#jsonobject" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.JSONObject">
<em class="property">class </em><code class="descname">JSONObject</code>(<em>**fields</em>)<a class="headerlink" href="#django.db.models.functions.JSONObject" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>Takes a list of key-value pairs and returns a JSON object containing those
pairs.</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">F</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">JSONObject</span><span class="p">,</span> <span class="n">Lower</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;msmith&#39;</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">25</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">json_object</span><span class="o">=</span><span class="n">JSONObject</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">name</span><span class="o">=</span><span class="n">Lower</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;alias&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">age</span><span class="o">=</span><span class="n">F</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">... </span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span><span class="o">.</span><span class="n">json_object</span>
<span class="go">{&#39;name&#39;: &#39;margaret smith&#39;, &#39;alias&#39;: &#39;msmith&#39;, &#39;age&#39;: 50}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-least">
<span id="least"></span><h3><code class="docutils literal notranslate"><span class="pre">Least</span></code><a class="headerlink" href="#least" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Least">
<em class="property">class </em><code class="descname">Least</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Least" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受至少两个字段名或表达式的列表，并返回最小值。每个参数必须是同样的类型，因此混合文本和数字将导致数据库错误。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>当一个或多个表达式可能是 <code class="docutils literal notranslate"><span class="pre">null</span></code> 时，<code class="docutils literal notranslate"><span class="pre">Least</span></code> 的行为在不同的数据库之间有所不同。</p>
<ul class="simple">
<li>PostgreSQL：<code class="docutils literal notranslate"><span class="pre">Least</span></code> 将返回最小的非空表达式，如果所有表达式都是 <code class="docutils literal notranslate"><span class="pre">null</span></code>，则返回 <code class="docutils literal notranslate"><span class="pre">null</span></code>。</li>
<li>SQLite、Oracle 和 MySQL。如果任何表达式是 <code class="docutils literal notranslate"><span class="pre">null</span></code>，<code class="docutils literal notranslate"><span class="pre">Least</span></code> 将返回 <code class="docutils literal notranslate"><span class="pre">null</span></code>。</li>
</ul>
<p class="last">如果你知道一个合理的最大值作为默认值，可以使用` <cite>Coalesce`</cite> 来模拟 PostgreSQL 的行为。</p>
</div>
</div>
<div class="section" id="s-nullif">
<span id="nullif"></span><h3><code class="docutils literal notranslate"><span class="pre">NullIf</span></code><a class="headerlink" href="#nullif" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.NullIf">
<em class="property">class </em><code class="descname">NullIf</code>(<em>expression1</em>, <em>expression2</em>)<a class="headerlink" href="#django.db.models.functions.NullIf" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受两个表达式，如果相等则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">expression1</span></code>。</p>
<div class="admonition-caveats-on-oracle admonition">
<p class="first admonition-title">关于 Oracle 的注意事项</p>
<p>由于 <a class="reference internal" href="../databases.html#oracle-null-empty-strings"><span class="std std-ref">Oracle 惯例</span></a>，当表达式为 <a class="reference internal" href="fields.html#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal notranslate"><span class="pre">CharField</span></code></a> 类型时，该函数返回空字符串而不是 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<p class="last">在 Oracle 上禁止将 <code class="docutils literal notranslate"><span class="pre">Value(None)</span></code> 传递给 <code class="docutils literal notranslate"><span class="pre">expression1</span></code>，因为 Oracle 不接受 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 作为第一个参数。</p>
</div>
</div>
</div>
<div class="section" id="s-date-functions">
<span id="s-id1"></span><span id="date-functions"></span><span id="id1"></span><h2>日期函数<a class="headerlink" href="#date-functions" title="永久链接至标题">¶</a></h2>
<p>我们将在每个函数的例子中使用以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Experiment</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">start_datetime</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">start_time</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TimeField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">end_datetime</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">end_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">end_time</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TimeField</span><span class="p">(</span><span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="s-extract">
<span id="extract"></span><h3><code class="docutils literal notranslate"><span class="pre">Extract</span></code><a class="headerlink" href="#extract" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Extract">
<em class="property">class </em><code class="descname">Extract</code>(<em>expression</em>, <em>lookup_name=None</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Extract" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>提取日期的一个组成部分作为一个数字。</p>
<p>取一个代表 <code class="docutils literal notranslate"><span class="pre">DateField</span></code>、<code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>、<code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DurationField</span></code> 和 <code class="docutils literal notranslate"><span class="pre">lookup_name</span></code> 的 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 和一个 <code class="docutils literal notranslate"><span class="pre">lookup_name</span></code> 引用的日期部分作为 <code class="docutils literal notranslate"><span class="pre">IntegerField</span></code> 返回。Django 通常使用数据库的 extract 函数，所以你可以使用任何数据库支持的 <code class="docutils literal notranslate"><span class="pre">lookup_name</span></code>。<code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> 子类，通常由 <code class="docutils literal notranslate"><span class="pre">pytz</span></code> 提供，可以通过提取特定时区的值。</p>
<p>给定日期时间 <code class="docutils literal notranslate"><span class="pre">2015-06-15</span> <span class="pre">23:30:01.000321+00:00</span></code>，内置的 <code class="docutils literal notranslate"><span class="pre">lookup_name</span></code> 返回。</p>
<ul class="simple">
<li>&quot;year&quot;: 2015</li>
<li>&quot;iso_year&quot;: 2015</li>
<li>&quot;quarter&quot;: 2</li>
<li>&quot;month&quot;: 6</li>
<li>&quot;day&quot;: 15</li>
<li>&quot;week&quot;: 25</li>
<li>&quot;week_day&quot;: 2</li>
<li>&quot;iso_week_day&quot;: 1</li>
<li>&quot;hour&quot;: 23</li>
<li>&quot;minute&quot;: 30</li>
<li>&quot;second&quot;: 1</li>
</ul>
<p>如果在 Django 中使用了不同的时区，比如 <code class="docutils literal notranslate"><span class="pre">Australia/Melbourne</span></code> ，那么在提取值之前，日期时间会被转换为该时区。在上面的例子中，墨尔本的时区偏移是 +10:00。当这个时区被激活时，返回的值将与上述相同，除了：</p>
<ul class="simple">
<li>&quot;day&quot;: 16</li>
<li>&quot;week_day&quot;: 3</li>
<li>&quot;iso_week_day&quot;: 2</li>
<li>&quot;hour&quot;: 9</li>
</ul>
<div class="admonition-week-day-values admonition">
<p class="first admonition-title"><code class="docutils literal notranslate"><span class="pre">week_day</span></code> 值</p>
<p><code class="docutils literal notranslate"><span class="pre">week_day</span></code> <code class="docutils literal notranslate"><span class="pre">lookup_type</span></code> 的计算方式与大多数数据库和 Python 的标准函数不同。这个函数将返回星期日的 <code class="docutils literal notranslate"><span class="pre">1</span></code>，星期一的 <code class="docutils literal notranslate"><span class="pre">2</span></code>，到星期六的 <code class="docutils literal notranslate"><span class="pre">7</span></code>。</p>
<p>在 Python 中的等价计算是：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">isoweekday</span><span class="p">()</span> <span class="o">%</span> <span class="mi">7</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">2</span>
</pre></div>
</div>
</div>
<div class="admonition-week-values admonition">
<p class="first admonition-title"><code class="docutils literal notranslate"><span class="pre">week</span></code> 值</p>
<p class="last">The <code class="docutils literal notranslate"><span class="pre">week</span></code> <code class="docutils literal notranslate"><span class="pre">lookup_type</span></code> is calculated based on <a class="reference external" href="https://en.wikipedia.org/wiki/ISO-8601">ISO-8601</a>, i.e.,
a week starts on a Monday. The first week of a year is the one that
contains the year's first Thursday, i.e. the first week has the majority
(four or more) of its days in the year. The value returned is in the range
1 to 52 or 53.</p>
</div>
<p>上面的每个 <code class="docutils literal notranslate"><span class="pre">lookup_name</span></code> 都有一个相应的 <code class="docutils literal notranslate"><span class="pre">Extract</span></code> 子类（下面列出的），通常应该用这个子类来代替比较啰嗦的等价物，例如，使用 <code class="docutils literal notranslate"><span class="pre">ExtractYear(...)</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">Extract(...，lookup_name='year')</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Extract</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_datetime</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start</span><span class="o">.</span><span class="n">date</span><span class="p">(),</span>
<span class="gp">... </span>   <span class="n">end_datetime</span><span class="o">=</span><span class="n">end</span><span class="p">,</span> <span class="n">end_date</span><span class="o">=</span><span class="n">end</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Add the experiment start year as a field in the QuerySet.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiment</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_year</span><span class="o">=</span><span class="n">Extract</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;year&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiment</span><span class="o">.</span><span class="n">start_year</span>
<span class="go">2015</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># How many experiments completed in the same year in which they started?</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_datetime__year</span><span class="o">=</span><span class="n">Extract</span><span class="p">(</span><span class="s1">&#39;end_datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;year&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="go">1</span>
</pre></div>
</div>
<div class="section" id="s-datefield-extracts">
<span id="datefield-extracts"></span><h4><code class="docutils literal notranslate"><span class="pre">DateField</span></code> 提取<a class="headerlink" href="#datefield-extracts" title="永久链接至标题">¶</a></h4>
<dl class="class">
<dt id="django.db.models.functions.ExtractYear">
<em class="property">class </em><code class="descname">ExtractYear</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractYear" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'year'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractIsoYear">
<em class="property">class </em><code class="descname">ExtractIsoYear</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractIsoYear" title="永久链接至目标">¶</a></dt>
<dd><p>返回 ISO-8601 的周号年份。</p>
<dl class="attribute">
<dt>
<code class="descname">lookup_name = 'iso_year'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractMonth">
<em class="property">class </em><code class="descname">ExtractMonth</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractMonth" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'month'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractDay">
<em class="property">class </em><code class="descname">ExtractDay</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractDay" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'day'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractWeekDay">
<em class="property">class </em><code class="descname">ExtractWeekDay</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractWeekDay" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'week_day'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractIsoWeekDay">
<em class="property">class </em><code class="descname">ExtractIsoWeekDay</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractIsoWeekDay" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.1.</span> </div>
<p>返回 ISO-8601 的星期日，第 1 天是星期一，第 7 天是星期天。</p>
<dl class="attribute">
<dt>
<code class="descname">lookup_name = 'iso_week_day'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractWeek">
<em class="property">class </em><code class="descname">ExtractWeek</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractWeek" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'week'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractQuarter">
<em class="property">class </em><code class="descname">ExtractQuarter</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractQuarter" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'quarter'</code></dt>
<dd></dd></dl>

</dd></dl>

<p>这些类在逻辑上等同于 <code class="docutils literal notranslate"><span class="pre">Extract('date_field',</span> <span class="pre">lookup_name)</span></code>。每个类也是一个 <code class="docutils literal notranslate"><span class="pre">Transform</span></code> 在 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 和 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 上注册为 <code class="docutils literal notranslate"><span class="pre">__(lookup_name)`</span></code>，例如 <code class="docutils literal notranslate"><span class="pre">__year</span></code>。</p>
<p>由于 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 没有时间部分，只有处理日期部分的 <code class="docutils literal notranslate"><span class="pre">Extract</span></code> 子类才能与 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="p">(</span>
<span class="gp">... </span>    <span class="n">ExtractDay</span><span class="p">,</span> <span class="n">ExtractMonth</span><span class="p">,</span> <span class="n">ExtractQuarter</span><span class="p">,</span> <span class="n">ExtractWeek</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">ExtractIsoWeekDay</span><span class="p">,</span> <span class="n">ExtractWeekDay</span><span class="p">,</span> <span class="n">ExtractIsoYear</span><span class="p">,</span> <span class="n">ExtractYear</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start_2015</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">end_2015</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_datetime</span><span class="o">=</span><span class="n">start_2015</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start_2015</span><span class="o">.</span><span class="n">date</span><span class="p">(),</span>
<span class="gp">... </span>   <span class="n">end_datetime</span><span class="o">=</span><span class="n">end_2015</span><span class="p">,</span> <span class="n">end_date</span><span class="o">=</span><span class="n">end_2015</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">isoyear</span><span class="o">=</span><span class="n">ExtractIsoYear</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">quarter</span><span class="o">=</span><span class="n">ExtractQuarter</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">month</span><span class="o">=</span><span class="n">ExtractMonth</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">week</span><span class="o">=</span><span class="n">ExtractWeek</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">day</span><span class="o">=</span><span class="n">ExtractDay</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">weekday</span><span class="o">=</span><span class="n">ExtractWeekDay</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">isoweekday</span><span class="o">=</span><span class="n">ExtractIsoWeekDay</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span>
<span class="gp">... </span>    <span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;isoyear&#39;</span><span class="p">,</span> <span class="s1">&#39;quarter&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;weekday&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s1">&#39;isoweekday&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">end_date__year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">))</span>
<span class="go">{&#39;year&#39;: 2015, &#39;isoyear&#39;: 2015, &#39;quarter&#39;: 2, &#39;month&#39;: 6, &#39;week&#39;: 25,</span>
<span class="go"> &#39;day&#39;: 15, &#39;weekday&#39;: 2, &#39;isoweekday&#39;: 1}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-datetimefield-extracts">
<span id="datetimefield-extracts"></span><h4><code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 提取<a class="headerlink" href="#datetimefield-extracts" title="永久链接至标题">¶</a></h4>
<p>除以下内容外，上述 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 的所有提取物也可用于``DateTimeField``。</p>
<dl class="class">
<dt id="django.db.models.functions.ExtractHour">
<em class="property">class </em><code class="descname">ExtractHour</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractHour" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'hour'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractMinute">
<em class="property">class </em><code class="descname">ExtractMinute</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractMinute" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'minute'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.ExtractSecond">
<em class="property">class </em><code class="descname">ExtractSecond</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ExtractSecond" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'second'</code></dt>
<dd></dd></dl>

</dd></dl>

<p>这些类在逻辑上等同于 <code class="docutils literal notranslate"><span class="pre">Extract('datetime_field',</span> <span class="pre">lookup_name)</span></code>。每个类也是一个 <code class="docutils literal notranslate"><span class="pre">Transform</span></code> 在 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 上注册为 <code class="docutils literal notranslate"><span class="pre">__(lookup_name)</span></code>，例如 <code class="docutils literal notranslate"><span class="pre">__minute</span></code>。</p>
<p><code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="p">(</span>
<span class="gp">... </span>    <span class="n">ExtractDay</span><span class="p">,</span> <span class="n">ExtractHour</span><span class="p">,</span> <span class="n">ExtractMinute</span><span class="p">,</span> <span class="n">ExtractMonth</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">ExtractQuarter</span><span class="p">,</span> <span class="n">ExtractSecond</span><span class="p">,</span> <span class="n">ExtractWeek</span><span class="p">,</span> <span class="n">ExtractIsoWeekDay</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">ExtractWeekDay</span><span class="p">,</span> <span class="n">ExtractIsoYear</span><span class="p">,</span> <span class="n">ExtractYear</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start_2015</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">end_2015</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_datetime</span><span class="o">=</span><span class="n">start_2015</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start_2015</span><span class="o">.</span><span class="n">date</span><span class="p">(),</span>
<span class="gp">... </span>   <span class="n">end_datetime</span><span class="o">=</span><span class="n">end_2015</span><span class="p">,</span> <span class="n">end_date</span><span class="o">=</span><span class="n">end_2015</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">isoyear</span><span class="o">=</span><span class="n">ExtractIsoYear</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">quarter</span><span class="o">=</span><span class="n">ExtractQuarter</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">month</span><span class="o">=</span><span class="n">ExtractMonth</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">week</span><span class="o">=</span><span class="n">ExtractWeek</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">day</span><span class="o">=</span><span class="n">ExtractDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">weekday</span><span class="o">=</span><span class="n">ExtractWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">isoweekday</span><span class="o">=</span><span class="n">ExtractIsoWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">hour</span><span class="o">=</span><span class="n">ExtractHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">minute</span><span class="o">=</span><span class="n">ExtractMinute</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">second</span><span class="o">=</span><span class="n">ExtractSecond</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span>
<span class="gp">... </span>    <span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;isoyear&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s1">&#39;weekday&#39;</span><span class="p">,</span> <span class="s1">&#39;isoweekday&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;second&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">end_datetime__year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">))</span>
<span class="go">{&#39;year&#39;: 2015, &#39;isoyear&#39;: 2015, &#39;quarter&#39;: 2, &#39;month&#39;: 6, &#39;week&#39;: 25,</span>
<span class="go"> &#39;day&#39;: 15, &#39;weekday&#39;: 2, &#39;isoweekday&#39;: 1, &#39;hour&#39;: 23, &#39;minute&#39;: 30,</span>
<span class="go"> &#39;second&#39;: 1}</span>
</pre></div>
</div>
<p>当 <a class="reference internal" href="../settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时，数据库中的日期时间是以 UTC 存储的。如果在 Django 中使用了不同的时区，那么在提取值之前，日期时间会被转换为该时区。下面的例子将转换为墨尔本时区（UTC +10:00），从而改变了返回的日期、星期和小时值：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pytz</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">melb</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;Australia/Melbourne&#39;</span><span class="p">)</span>  <span class="c1"># UTC+10:00</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">timezone</span><span class="o">.</span><span class="n">override</span><span class="p">(</span><span class="n">melb</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>       <span class="n">day</span><span class="o">=</span><span class="n">ExtractDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>       <span class="n">weekday</span><span class="o">=</span><span class="n">ExtractWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>       <span class="n">isoweekday</span><span class="o">=</span><span class="n">ExtractIsoWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>       <span class="n">hour</span><span class="o">=</span><span class="n">ExtractHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>   <span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;weekday&#39;</span><span class="p">,</span> <span class="s1">&#39;isoweekday&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
<span class="gp">... </span>       <span class="n">end_datetime__year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>   <span class="p">)</span>
<span class="go">{&#39;day&#39;: 16, &#39;weekday&#39;: 3, &#39;isoweekday&#39;: 2, &#39;hour&#39;: 9}</span>
</pre></div>
</div>
<p>明确地将时区传递给 <code class="docutils literal notranslate"><span class="pre">Extract</span></code> 函数的行为与此相同，并优先于活动时区：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pytz</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">melb</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;Australia/Melbourne&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">day</span><span class="o">=</span><span class="n">ExtractDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">weekday</span><span class="o">=</span><span class="n">ExtractWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">isoweekday</span><span class="o">=</span><span class="n">ExtractIsoWeekDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">hour</span><span class="o">=</span><span class="n">ExtractHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;weekday&#39;</span><span class="p">,</span> <span class="s1">&#39;isoweekday&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">end_datetime__year</span><span class="o">=</span><span class="n">ExtractYear</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span>
<span class="go">{&#39;day&#39;: 16, &#39;weekday&#39;: 3, &#39;isoweekday&#39;: 2, &#39;hour&#39;: 9}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-now">
<span id="now"></span><h3><code class="docutils literal notranslate"><span class="pre">Now</span></code><a class="headerlink" href="#now" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Now">
<em class="property">class </em><code class="descname">Now</code><a class="headerlink" href="#django.db.models.functions.Now" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回数据库服务器执行查询时的当前日期和时间，通常使用 SQL <code class="docutils literal notranslate"><span class="pre">CURRENT_TIMESTAMP</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Now</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">published__lte</span><span class="o">=</span><span class="n">Now</span><span class="p">())</span>
<span class="go">&lt;QuerySet [&lt;Article: How to Django&gt;]&gt;</span>
</pre></div>
</div>
<div class="admonition-postgresql-considerations admonition">
<p class="first admonition-title">PostgreSQL 的注意事项</p>
<p class="last">在 PostgreSQL 中，SQL <code class="docutils literal notranslate"><span class="pre">CURRENT_TIMESTAMP</span></code> 返回的是当前事务开始的时间，因此为了跨数据库的兼容性，<code class="docutils literal notranslate"><span class="pre">Now()</span></code> 使用 <code class="docutils literal notranslate"><span class="pre">STATEMENT_TIMESTAMP</span></code> 代替。因此为了跨数据库的兼容性，<code class="docutils literal notranslate"><span class="pre">Now()</span></code> 使用 <code class="docutils literal notranslate"><span class="pre">STATEMENT_TIMESTAMP</span></code> 代替。如果需要事务时间戳，可以使用 <code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.postgres.function.TransactionNow</span></code>。</p>
</div>
</div>
<div class="section" id="s-trunc">
<span id="trunc"></span><h3><code class="docutils literal notranslate"><span class="pre">Trunc</span></code><a class="headerlink" href="#trunc" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Trunc">
<em class="property">class </em><code class="descname">Trunc</code>(<em>expression</em>, <em>kind</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Trunc" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>将一个日期截断到一个重要的部分。</p>
<p>当你只关心某事是否发生在某年、某小时或某天，而不关心确切的秒数时，那么 <code class="docutils literal notranslate"><span class="pre">Trunc</span></code> （及其子类）可以用来过滤或汇总你的数据。例如，你可以使用 <code class="docutils literal notranslate"><span class="pre">Trunc</span></code> 来计算每天的销售数量。</p>
<p><code class="docutils literal notranslate"><span class="pre">Trunc</span></code> 接受一个 <code class="docutils literal notranslate"><span class="pre">expression</span></code>，表示 <code class="docutils literal notranslate"><span class="pre">DateField</span></code>、<code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>，一个 <code class="docutils literal notranslate"><span class="pre">kind</span></code> 表示日期或时间部分，一个 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 是 <code class="docutils literal notranslate"><span class="pre">DateTimeField()`</span></code>、<code class="docutils literal notranslate"><span class="pre">TimeField()</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DateField()</span></code>。它根据 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 返回一个日期时间、日期或时间，其中 <code class="docutils literal notranslate"><span class="pre">kind</span></code> 以下的字段设置为最小值。如果省略了 <code class="docutils literal notranslate"><span class="pre">output_field</span></code>，它将默认为 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 的 <code class="docutils literal notranslate"><span class="pre">output_field</span></code>。<code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> 子类，通常由 <code class="docutils literal notranslate"><span class="pre">pytz</span></code> 提供，可以通过传递来截断特定时区的值。</p>
<p><code class="docutils literal notranslate"><span class="pre">is_dst</span></code> 参数表示 <code class="docutils literal notranslate"><span class="pre">pytz</span></code> 是否应该解释夏令时中不存在的和模糊的日期。默认情况下（当 <code class="docutils literal notranslate"><span class="pre">is_dst=None</span></code>），<code class="docutils literal notranslate"><span class="pre">pytz</span></code> 会对这种日期时间产生异常。</p>
<p>给定日期时间 <code class="docutils literal notranslate"><span class="pre">2015-06-15</span> <span class="pre">14:30:50.000321+00:00</span></code>，内置 <code class="docutils literal notranslate"><span class="pre">kind</span></code> 返回：</p>
<ul class="simple">
<li>&quot;year&quot;: 2015-01-01 00:00:00+00:00</li>
<li>&quot;quarter&quot;: 2015-04-01 00:00:00+00:00</li>
<li>&quot;month&quot;: 2015-06-01 00:00:00+00:00</li>
<li>&quot;week&quot;: 2015-06-15 00:00:00+00:00</li>
<li>&quot;day&quot;: 2015-06-15 00:00:00+00:00</li>
<li>&quot;hour&quot;: 2015-06-15 14:00:00+00:00</li>
<li>&quot;minute&quot;: 2015-06-15 14:30:00+00:00</li>
<li>&quot;second&quot;: 2015-06-15 14:30:50+00:00</li>
</ul>
<p>如果在 Django 中使用了不同的时区，比如 <code class="docutils literal notranslate"><span class="pre">Australia/Melbourne</span></code>，那么日期时间会在被截断之前转换为新的时区。在上面的例子中，墨尔本的时区偏移是 +10:00。当这个时区被激活时，返回的值将是：</p>
<ul class="simple">
<li>&quot;year&quot;: 2015-01-01 00:00:00+11:00</li>
<li>&quot;quarter&quot;: 2015-04-01 00:00:00+10:00</li>
<li>&quot;month&quot;: 2015-06-01 00:00:00+10:00</li>
<li>&quot;week&quot;: 2015-06-16 00:00:00+10:00</li>
<li>&quot;day&quot;: 2015-06-16 00:00:00+10:00</li>
<li>&quot;hour&quot;: 2015-06-16 00:00:00+10:00</li>
<li>&quot;minute&quot;: 2015-06-16 00:30:00+10:00</li>
<li>&quot;second&quot;: 2015-06-16 00:30:50+10:00</li>
</ul>
<p>年的偏移量为 +11:00，因为结果过渡到夏令时。</p>
<p>以上每个 <code class="docutils literal notranslate"><span class="pre">kind</span></code> 都有一个对应的 <code class="docutils literal notranslate"><span class="pre">Trunc</span></code> 子类（下面列出的），通常应该用这个子类来代替比较啰嗦的等价物，例如使用 <code class="docutils literal notranslate"><span class="pre">TruncYear(...)</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">Trunc(...，kind='year')</span></code>。</p>
<p>子类都被定义为变换，但它们没有注册任何字段，因为查找名称已经被 <code class="docutils literal notranslate"><span class="pre">Extract</span></code> 子类保留。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Count</span><span class="p">,</span> <span class="n">DateTimeField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Trunc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">321</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">123</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">999</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments_per_day</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_day</span><span class="o">=</span><span class="n">Trunc</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">DateTimeField</span><span class="p">())</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;start_day&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">experiments</span><span class="o">=</span><span class="n">Count</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments_per_day</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="p">[</span><span class="s1">&#39;start_day&#39;</span><span class="p">],</span> <span class="n">exp</span><span class="p">[</span><span class="s1">&#39;experiments&#39;</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">2015-06-15 00:00:00 2</span>
<span class="go">2015-12-25 00:00:00 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">start_day</span><span class="o">=</span><span class="n">Trunc</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">DateTimeField</span><span class="p">())</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">start_day</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="o">.</span><span class="n">start_datetime</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">2015-06-15 14:30:50.000321</span>
<span class="go">2015-06-15 14:40:02.000123</span>
</pre></div>
</div>
<div class="section" id="s-datefield-truncation">
<span id="datefield-truncation"></span><h4><code class="docutils literal notranslate"><span class="pre">DateField</span></code> 截断<a class="headerlink" href="#datefield-truncation" title="永久链接至标题">¶</a></h4>
<dl class="class">
<dt id="django.db.models.functions.TruncYear">
<em class="property">class </em><code class="descname">TruncYear</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncYear" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'year'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncMonth">
<em class="property">class </em><code class="descname">TruncMonth</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncMonth" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'month'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncWeek">
<em class="property">class </em><code class="descname">TruncWeek</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncWeek" title="永久链接至目标">¶</a></dt>
<dd><p>截断到每周一的午夜。</p>
<dl class="attribute">
<dt>
<code class="descname">kind = 'week'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncQuarter">
<em class="property">class </em><code class="descname">TruncQuarter</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncQuarter" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'quarter'</code></dt>
<dd></dd></dl>

</dd></dl>

<p>这些在逻辑上等同于 <code class="docutils literal notranslate"><span class="pre">Trunc('date_field',</span> <span class="pre">kind)</span></code>。它们截断日期的所有部分，直至 <code class="docutils literal notranslate"><span class="pre">kind</span></code>，允许以较低的精度对日期进行分组或过滤。<code class="docutils literal notranslate"><span class="pre">expression</span></code> 可以有一个 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 的 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>。</p>
<p>由于 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 没有时间部分，只有处理日期部分的 <code class="docutils literal notranslate"><span class="pre">Trunc</span></code> 子类才能与 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">TruncMonth</span><span class="p">,</span> <span class="n">TruncYear</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">321</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start3</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">999</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start1</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start1</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start2</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start2</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start3</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start3</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments_per_year</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">year</span><span class="o">=</span><span class="n">TruncYear</span><span class="p">(</span><span class="s1">&#39;start_date&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;year&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">experiments</span><span class="o">=</span><span class="n">Count</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments_per_year</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="p">[</span><span class="s1">&#39;year&#39;</span><span class="p">],</span> <span class="n">exp</span><span class="p">[</span><span class="s1">&#39;experiments&#39;</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">2014-01-01 1</span>
<span class="go">2015-01-01 2</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pytz</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">melb</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;Australia/Melbourne&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments_per_month</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">month</span><span class="o">=</span><span class="n">TruncMonth</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">))</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;month&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">experiments</span><span class="o">=</span><span class="n">Count</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments_per_month</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="p">[</span><span class="s1">&#39;month&#39;</span><span class="p">],</span> <span class="n">exp</span><span class="p">[</span><span class="s1">&#39;experiments&#39;</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">2015-06-01 00:00:00+10:00 1</span>
<span class="go">2016-01-01 00:00:00+11:00 1</span>
<span class="go">2014-06-01 00:00:00+10:00 1</span>
</pre></div>
</div>
</div>
<div class="section" id="s-datetimefield-truncation">
<span id="datetimefield-truncation"></span><h4><code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 截断<a class="headerlink" href="#datetimefield-truncation" title="永久链接至标题">¶</a></h4>
<dl class="class">
<dt id="django.db.models.functions.TruncDate">
<em class="property">class </em><code class="descname">TruncDate</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncDate" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'date'</code></dt>
<dd></dd></dl>

<dl class="attribute">
<dt>
<code class="descname">output_field = DateField()</code></dt>
<dd></dd></dl>

<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>The <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> parameter was added.</p>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">TruncDate</span></code> 将 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 投射到一个日期，而不是使用内置的 SQL truncate 函数。在 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 上，它也被注册为 <code class="docutils literal notranslate"><span class="pre">__date</span></code> 的转换。</p>
<dl class="class">
<dt id="django.db.models.functions.TruncTime">
<em class="property">class </em><code class="descname">TruncTime</code>(<em>expression</em>, <em>tzinfo=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncTime" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">lookup_name = 'time'</code></dt>
<dd></dd></dl>

<dl class="attribute">
<dt>
<code class="descname">output_field = TimeField()</code></dt>
<dd></dd></dl>

<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>The <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> parameter was added.</p>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">TruncTime</span></code> 将 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 投射到一个时间，而不是使用内置的 SQL truncate 函数。在 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 上，它也被注册为 <code class="docutils literal notranslate"><span class="pre">__time</span></code> 的转换。</p>
<dl class="class">
<dt id="django.db.models.functions.TruncDay">
<em class="property">class </em><code class="descname">TruncDay</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncDay" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'day'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncHour">
<em class="property">class </em><code class="descname">TruncHour</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncHour" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'hour'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncMinute">
<em class="property">class </em><code class="descname">TruncMinute</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncMinute" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'minute'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="django.db.models.functions.TruncSecond">
<em class="property">class </em><code class="descname">TruncSecond</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.TruncSecond" title="永久链接至目标">¶</a></dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'second'</code></dt>
<dd></dd></dl>

</dd></dl>

<p>这些在逻辑上等同于 <code class="docutils literal notranslate"><span class="pre">Trunc('datetime_field',</span> <span class="pre">kind)</span></code>。它们截断日期的所有部分，直至 <code class="docutils literal notranslate"><span class="pre">kind</span></code>，并允许以较低的精度对日期时间进行分组或过滤。<code class="docutils literal notranslate"><span class="pre">expression</span></code> 必须有一个 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 的 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span><span class="p">,</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="p">(</span>
<span class="gp">... </span>    <span class="n">TruncDate</span><span class="p">,</span> <span class="n">TruncDay</span><span class="p">,</span> <span class="n">TruncHour</span><span class="p">,</span> <span class="n">TruncMinute</span><span class="p">,</span> <span class="n">TruncSecond</span><span class="p">,</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pytz</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">321</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start1</span><span class="p">,</span> <span class="n">start_date</span><span class="o">=</span><span class="n">start1</span><span class="o">.</span><span class="n">date</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">melb</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;Australia/Melbourne&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">date</span><span class="o">=</span><span class="n">TruncDate</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">day</span><span class="o">=</span><span class="n">TruncDay</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">hour</span><span class="o">=</span><span class="n">TruncHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">minute</span><span class="o">=</span><span class="n">TruncMinute</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">second</span><span class="o">=</span><span class="n">TruncSecond</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;second&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="go">{&#39;date&#39;: datetime.date(2014, 6, 15),</span>
<span class="go"> &#39;day&#39;: datetime.datetime(2014, 6, 16, 0, 0, tzinfo=&lt;DstTzInfo &#39;Australia/Melbourne&#39; AEST+10:00:00 STD&gt;),</span>
<span class="go"> &#39;hour&#39;: datetime.datetime(2014, 6, 16, 0, 0, tzinfo=&lt;DstTzInfo &#39;Australia/Melbourne&#39; AEST+10:00:00 STD&gt;),</span>
<span class="go"> &#39;minute&#39;: &#39;minute&#39;: datetime.datetime(2014, 6, 15, 14, 30, tzinfo=&lt;UTC&gt;),</span>
<span class="go"> &#39;second&#39;: datetime.datetime(2014, 6, 15, 14, 30, 50, tzinfo=&lt;UTC&gt;)</span>
<span class="go">}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-timefield-truncation">
<span id="timefield-truncation"></span><h4><code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 截断<a class="headerlink" href="#timefield-truncation" title="永久链接至标题">¶</a></h4>
<dl class="class">
<dt>
<em class="property">class </em><code class="descname">TruncHour</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)</dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'hour'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="descname">TruncMinute</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)</dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'minute'</code></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="descname">TruncSecond</code>(<em>expression</em>, <em>output_field=None</em>, <em>tzinfo=None</em>, <em>is_dst=None</em>, <em>**extra</em>)</dt>
<dd><dl class="attribute">
<dt>
<code class="descname">kind = 'second'</code></dt>
<dd></dd></dl>

</dd></dl>

<p>这些在逻辑上等同于 <code class="docutils literal notranslate"><span class="pre">Trunc('time_field',</span> <span class="pre">kind)</span></code>。它们截断时间的所有部分，直至 <code class="docutils literal notranslate"><span class="pre">kind</span></code>，这就允许以较低的精度对时间进行分组或过滤。<code class="docutils literal notranslate"><span class="pre">expression</span></code> 可以有一个 <code class="docutils literal notranslate"><span class="pre">output_field</span></code> 的 <code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>。</p>
<p>由于 <code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 没有日期部分，只有处理时间部分的 <code class="docutils literal notranslate"><span class="pre">Trunc</span></code> 子类可以与 <code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Count</span><span class="p">,</span> <span class="n">TimeField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">TruncHour</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">321</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">start3</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">999</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start1</span><span class="p">,</span> <span class="n">start_time</span><span class="o">=</span><span class="n">start1</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start2</span><span class="p">,</span> <span class="n">start_time</span><span class="o">=</span><span class="n">start2</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">start_datetime</span><span class="o">=</span><span class="n">start3</span><span class="p">,</span> <span class="n">start_time</span><span class="o">=</span><span class="n">start3</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments_per_hour</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">hour</span><span class="o">=</span><span class="n">TruncHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">TimeField</span><span class="p">()),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;hour&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">experiments</span><span class="o">=</span><span class="n">Count</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments_per_hour</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="p">[</span><span class="s1">&#39;hour&#39;</span><span class="p">],</span> <span class="n">exp</span><span class="p">[</span><span class="s1">&#39;experiments&#39;</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">14:00:00 2</span>
<span class="go">17:00:00 1</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pytz</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">melb</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&#39;Australia/Melbourne&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">experiments_per_hour</span> <span class="o">=</span> <span class="n">Experiment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">hour</span><span class="o">=</span><span class="n">TruncHour</span><span class="p">(</span><span class="s1">&#39;start_datetime&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">melb</span><span class="p">),</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;hour&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">experiments</span><span class="o">=</span><span class="n">Count</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">experiments_per_hour</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">exp</span><span class="p">[</span><span class="s1">&#39;hour&#39;</span><span class="p">],</span> <span class="n">exp</span><span class="p">[</span><span class="s1">&#39;experiments&#39;</span><span class="p">])</span>
<span class="gp">...</span>
<span class="go">2014-06-16 00:00:00+10:00 2</span>
<span class="go">2016-01-01 04:00:00+11:00 1</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="s-math-functions">
<span id="s-id2"></span><span id="math-functions"></span><span id="id2"></span><h2>数学函数<a class="headerlink" href="#math-functions" title="永久链接至标题">¶</a></h2>
<p>我们将在数学函数实例中使用以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Vector</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FloatField</span><span class="p">()</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FloatField</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="s-abs">
<span id="abs"></span><h3><code class="docutils literal notranslate"><span class="pre">Abs</span></code><a class="headerlink" href="#abs" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Abs">
<em class="property">class </em><code class="descname">Abs</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Abs" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的绝对值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Abs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_abs</span><span class="o">=</span><span class="n">Abs</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_abs</span><span class="o">=</span><span class="n">Abs</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_abs</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_abs</span>
<span class="go">(0.5, 1.1)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Abs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Abs</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors inside the unit cube</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__abs__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__abs__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-acos">
<span id="acos"></span><h3><code class="docutils literal notranslate"><span class="pre">ACos</span></code><a class="headerlink" href="#acos" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.ACos">
<em class="property">class </em><code class="descname">ACos</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ACos" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的余弦值。表达式的值必须在 -1 到 1 的范围内。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ACos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">0.9</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_acos</span><span class="o">=</span><span class="n">ACos</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_acos</span><span class="o">=</span><span class="n">ACos</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_acos</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_acos</span>
<span class="go">(1.0471975511965979, 2.6905658417935308)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ACos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">ACos</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose arccosine is less than 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__acos__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__acos__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-asin">
<span id="asin"></span><h3><code class="docutils literal notranslate"><span class="pre">ASin</span></code><a class="headerlink" href="#asin" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.ASin">
<em class="property">class </em><code class="descname">ASin</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ASin" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的正弦值。表达式的值必须在 -1 到 1 的范围内。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ASin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_asin</span><span class="o">=</span><span class="n">ASin</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_asin</span><span class="o">=</span><span class="n">ASin</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_asin</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_asin</span>
<span class="go">(0.0, 1.5707963267948966)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ASin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">ASin</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose arcsine is less than 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__asin__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__asin__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-atan">
<span id="atan"></span><h3><code class="docutils literal notranslate"><span class="pre">ATan</span></code><a class="headerlink" href="#atan" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.ATan">
<em class="property">class </em><code class="descname">ATan</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ATan" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的正切值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ATan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">3.12</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">6.987</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_atan</span><span class="o">=</span><span class="n">ATan</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_atan</span><span class="o">=</span><span class="n">ATan</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_atan</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_atan</span>
<span class="go">(1.2606282660069106, 1.428638798133829)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ATan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">ATan</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose arctangent is less than 2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__atan__lt</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">y__atan__lt</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-atan2">
<span id="atan2"></span><h3><code class="docutils literal notranslate"><span class="pre">ATan2</span></code><a class="headerlink" href="#atan2" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.ATan2">
<em class="property">class </em><code class="descname">ATan2</code>(<em>expression1</em>, <em>expression2</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.ATan2" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回 <code class="docutils literal notranslate"><span class="pre">expression1</span> <span class="pre">/</span> <span class="pre">expression2</span></code> 的正切值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">ATan2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">2.5</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.9</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">atan2</span><span class="o">=</span><span class="n">ATan2</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">atan2</span>
<span class="go">0.9209258773829491</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ceil">
<span id="ceil"></span><h3><code class="docutils literal notranslate"><span class="pre">Ceil</span></code><a class="headerlink" href="#ceil" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Ceil">
<em class="property">class </em><code class="descname">Ceil</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Ceil" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回大于或等于一个数值字段或表达式的最小整数。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Ceil</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">3.12</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">7.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_ceil</span><span class="o">=</span><span class="n">Ceil</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_ceil</span><span class="o">=</span><span class="n">Ceil</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_ceil</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_ceil</span>
<span class="go">(4.0, 7.0)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Ceil</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Ceil</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose ceil is less than 10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__ceil__lt</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">y__ceil__lt</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-cos">
<span id="cos"></span><h3><code class="docutils literal notranslate"><span class="pre">Cos</span></code><a class="headerlink" href="#cos" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Cos">
<em class="property">class </em><code class="descname">Cos</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Cos" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的余弦值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=-</span><span class="mf">8.0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">3.1415926</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_cos</span><span class="o">=</span><span class="n">Cos</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_cos</span><span class="o">=</span><span class="n">Cos</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_cos</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_cos</span>
<span class="go">(-0.14550003380861354, -0.9999999999999986)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cos</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Cos</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose cosine is less than 0.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__cos__lt</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">y__cos__lt</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-cot">
<span id="cot"></span><h3><code class="docutils literal notranslate"><span class="pre">Cot</span></code><a class="headerlink" href="#cot" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Cot">
<em class="property">class </em><code class="descname">Cot</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Cot" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回数值字段或表达式的正切值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cot</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">12.0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_cot</span><span class="o">=</span><span class="n">Cot</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_cot</span><span class="o">=</span><span class="n">Cot</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_cot</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_cot</span>
<span class="go">(-1.5726734063976826, 0.642092615934331)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Cot</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Cot</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose cotangent is less than 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__cot__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__cot__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-degrees">
<span id="degrees"></span><h3><code class="docutils literal notranslate"><span class="pre">Degrees</span></code><a class="headerlink" href="#degrees" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Degrees">
<em class="property">class </em><code class="descname">Degrees</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Degrees" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>将数值字段或表达式从弧度转换为度。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Degrees</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=-</span><span class="mf">1.57</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">3.14</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_d</span><span class="o">=</span><span class="n">Degrees</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_d</span><span class="o">=</span><span class="n">Degrees</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_d</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_d</span>
<span class="go">(-89.95437383553924, 179.9087476710785)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Degrees</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Degrees</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose degrees are less than 360</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__degrees__lt</span><span class="o">=</span><span class="mi">360</span><span class="p">,</span> <span class="n">y__degrees__lt</span><span class="o">=</span><span class="mi">360</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-exp">
<span id="exp"></span><h3><code class="docutils literal notranslate"><span class="pre">Exp</span></code><a class="headerlink" href="#exp" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Exp">
<em class="property">class </em><code class="descname">Exp</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Exp" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回 <code class="docutils literal notranslate"><span class="pre">e</span></code> （自然对数基数）的值，将其升为一个数值字段或表达式的幂。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">2.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_exp</span><span class="o">=</span><span class="n">Exp</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_exp</span><span class="o">=</span><span class="n">Exp</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_exp</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_exp</span>
<span class="go">(221.40641620418717, 0.1353352832366127)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Exp</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose exp() is greater than 10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__exp__gt</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">y__exp__gt</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-floor">
<span id="floor"></span><h3><code class="docutils literal notranslate"><span class="pre">Floor</span></code><a class="headerlink" href="#floor" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Floor">
<em class="property">class </em><code class="descname">Floor</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Floor" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回不大于数值字段或表达式的最大整数值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Floor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">2.3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_floor</span><span class="o">=</span><span class="n">Floor</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_floor</span><span class="o">=</span><span class="n">Floor</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_floor</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_floor</span>
<span class="go">(5.0, -3.0)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Floor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Floor</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose floor() is greater than 10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__floor__gt</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">y__floor__gt</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ln">
<span id="ln"></span><h3><code class="docutils literal notranslate"><span class="pre">Ln</span></code><a class="headerlink" href="#ln" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Ln">
<em class="property">class </em><code class="descname">Ln</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Ln" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的自然对数。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Ln</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">233.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_ln</span><span class="o">=</span><span class="n">Ln</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_ln</span><span class="o">=</span><span class="n">Ln</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_ln</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_ln</span>
<span class="go">(1.6863989535702288, 5.4510384535657)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Ln</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Ln</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose value greater than e</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__ln__gt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__ln__gt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-log">
<span id="log"></span><h3><code class="docutils literal notranslate"><span class="pre">Log</span></code><a class="headerlink" href="#log" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Log">
<em class="property">class </em><code class="descname">Log</code>(<em>expression1</em>, <em>expression2</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Log" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受两个数字字段或表达式，并返回第一个字段的对数到第二个字段的基数。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">2.0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">4.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">log</span><span class="o">=</span><span class="n">Log</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">log</span>
<span class="go">2.0</span>
</pre></div>
</div>
</div>
<div class="section" id="s-mod">
<span id="mod"></span><h3><code class="docutils literal notranslate"><span class="pre">Mod</span></code><a class="headerlink" href="#mod" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Mod">
<em class="property">class </em><code class="descname">Mod</code>(<em>expression1</em>, <em>expression2</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Mod" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受两个数值字段或表达式，并返回第一个字段除以第二个字段的余数（模数运算）。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Mod</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">2.3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">mod</span><span class="o">=</span><span class="n">Mod</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">mod</span>
<span class="go">0.8</span>
</pre></div>
</div>
</div>
<div class="section" id="s-pi">
<span id="pi"></span><h3><code class="docutils literal notranslate"><span class="pre">Pi</span></code><a class="headerlink" href="#pi" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Pi">
<em class="property">class </em><code class="descname">Pi</code>(<em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Pi" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回数学常数 <code class="docutils literal notranslate"><span class="pre">π</span></code> 的值。</p>
</div>
<div class="section" id="s-power">
<span id="power"></span><h3><code class="docutils literal notranslate"><span class="pre">Power</span></code><a class="headerlink" href="#power" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Power">
<em class="property">class </em><code class="descname">Power</code>(<em>expression1</em>, <em>expression2</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Power" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受两个数值字段或表达式，并将第一个字段的值提高到第二个字段的幂。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Power</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">power</span><span class="o">=</span><span class="n">Power</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">power</span>
<span class="go">0.25</span>
</pre></div>
</div>
</div>
<div class="section" id="s-radians">
<span id="radians"></span><h3><code class="docutils literal notranslate"><span class="pre">Radians</span></code><a class="headerlink" href="#radians" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Radians">
<em class="property">class </em><code class="descname">Radians</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Radians" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>将数值字段或表达式从度数转换为弧度。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Radians</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=-</span><span class="mi">90</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">180</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_r</span><span class="o">=</span><span class="n">Radians</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_r</span><span class="o">=</span><span class="n">Radians</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_r</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_r</span>
<span class="go">(-1.5707963267948966, 3.141592653589793)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Radians</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Radians</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose radians are less than 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__radians__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y__radians__lt</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-random">
<span id="random"></span><h3><code class="docutils literal notranslate"><span class="pre">Random</span></code><a class="headerlink" href="#random" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Random">
<em class="property">class </em><code class="descname">Random</code>(<em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Random" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>Returns a random value in the range <code class="docutils literal notranslate"><span class="pre">0.0</span> <span class="pre">≤</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">1.0</span></code>.</p>
</div>
<div class="section" id="s-round">
<span id="round"></span><h3><code class="docutils literal notranslate"><span class="pre">Round</span></code><a class="headerlink" href="#round" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Round">
<em class="property">class </em><code class="descname">Round</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Round" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>将一个数值字段或表达式四舍五入到最接近的整数。半数值是向上还是向下取整取决于数据库。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Round</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">2.3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_r</span><span class="o">=</span><span class="n">Round</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_r</span><span class="o">=</span><span class="n">Round</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_r</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_r</span>
<span class="go">(5.0, -2.0)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Round</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Round</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose round() is less than 20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__round__lt</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">y__round__lt</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-sign">
<span id="sign"></span><h3><code class="docutils literal notranslate"><span class="pre">Sign</span></code><a class="headerlink" href="#sign" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Sign">
<em class="property">class </em><code class="descname">Sign</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Sign" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数字字段或表达式的符号（-1，0，1）。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sign</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">2.3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_sign</span><span class="o">=</span><span class="n">Sign</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_sign</span><span class="o">=</span><span class="n">Sign</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_sign</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_sign</span>
<span class="go">(1, -1)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sign</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Sign</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose signs of components are less than 0.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__sign__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y__sign__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-sin">
<span id="sin"></span><h3><code class="docutils literal notranslate"><span class="pre">Sin</span></code><a class="headerlink" href="#sin" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Sin">
<em class="property">class </em><code class="descname">Sin</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Sin" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的正弦值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">5.4</span><span class="p">,</span> <span class="n">y</span><span class="o">=-</span><span class="mf">2.3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_sin</span><span class="o">=</span><span class="n">Sin</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_sin</span><span class="o">=</span><span class="n">Sin</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_sin</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_sin</span>
<span class="go">(-0.7727644875559871, -0.7457052121767203)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Sin</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose sin() is less than 0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__sin__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y__sin__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-sqrt">
<span id="sqrt"></span><h3><code class="docutils literal notranslate"><span class="pre">Sqrt</span></code><a class="headerlink" href="#sqrt" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Sqrt">
<em class="property">class </em><code class="descname">Sqrt</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Sqrt" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个非负数值字段或表达式的平方根。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mf">4.0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">12.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_sqrt</span><span class="o">=</span><span class="n">Sqrt</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_sqrt</span><span class="o">=</span><span class="n">Sqrt</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_sqrt</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_sqrt</span>
<span class="go">(2.0, 3.46410)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Sqrt</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose sqrt() is less than 5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__sqrt__lt</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">y__sqrt__lt</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-tan">
<span id="tan"></span><h3><code class="docutils literal notranslate"><span class="pre">Tan</span></code><a class="headerlink" href="#tan" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Tan">
<em class="property">class </em><code class="descname">Tan</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Tan" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个数值字段或表达式的正切值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Tan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">x_tan</span><span class="o">=</span><span class="n">Tan</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">y_tan</span><span class="o">=</span><span class="n">Tan</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vector</span><span class="o">.</span><span class="n">x_tan</span><span class="p">,</span> <span class="n">vector</span><span class="o">.</span><span class="n">y_tan</span>
<span class="go">(0.0, -0.6358599286615808)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">FloatField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Tan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Tan</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get vectors whose tangent is less than 0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vectors</span> <span class="o">=</span> <span class="n">Vector</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">x__tan__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y__tan__lt</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-text-functions">
<span id="s-id3"></span><span id="text-functions"></span><span id="id3"></span><h2>文本函数<a class="headerlink" href="#text-functions" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-chr">
<span id="chr"></span><h3><code class="docutils literal notranslate"><span class="pre">Chr</span></code><a class="headerlink" href="#chr" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Chr">
<em class="property">class </em><code class="descname">Chr</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Chr" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受一个数值字段或表达式，并将表达式的文本表示形式作为单个字符返回。它的工作原理与 Python 的 <a class="reference external" href="https://docs.python.org/3/library/functions.html#chr" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">chr()</span></code></a> 函数相同。</p>
<p>和 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 一样，它也可以在 <code class="docutils literal notranslate"><span class="pre">IntegerField</span></code> 上作为变换注册。默认的查询名是 <code class="docutils literal notranslate"><span class="pre">chr</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Chr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__startswith</span><span class="o">=</span><span class="n">Chr</span><span class="p">(</span><span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;M&#39;</span><span class="p">)))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">Margaret Smith</span>
</pre></div>
</div>
</div>
<div class="section" id="s-concat">
<span id="concat"></span><h3><code class="docutils literal notranslate"><span class="pre">Concat</span></code><a class="headerlink" href="#concat" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Concat">
<em class="property">class </em><code class="descname">Concat</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Concat" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受至少两个文本字段或表达式的列表，并返回连接后的文本。每个参数必须是文本或字符类型。如果你想把一个 <code class="docutils literal notranslate"><span class="pre">TextField()</span></code> 和一个 <code class="docutils literal notranslate"><span class="pre">CharField()</span></code> 连接起来，那么一定要告诉 Django，<code class="docutils literal notranslate"><span class="pre">output_field</span></code> 应该是一个 <code class="docutils literal notranslate"><span class="pre">TextField()</span></code>。当连接一个 <code class="docutils literal notranslate"><span class="pre">Value</span></code> 时，也需要指定一个 <code class="docutils literal notranslate"><span class="pre">output_field</span></code>，如下面的例子。</p>
<p>这个函数永远不会有一个空的结果。在后端，如果一个空参数导致整个表达式为空，Django 会确保每个空的部分先转换成空字符串。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get the display name as &quot;name (goes_by)&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">CharField</span><span class="p">,</span> <span class="n">Value</span> <span class="k">as</span> <span class="n">V</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Concat</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">,</span> <span class="n">goes_by</span><span class="o">=</span><span class="s1">&#39;Maggie&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">screen_name</span><span class="o">=</span><span class="n">Concat</span><span class="p">(</span>
<span class="gp">... </span>        <span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">V</span><span class="p">(</span><span class="s1">&#39; (&#39;</span><span class="p">),</span> <span class="s1">&#39;goes_by&#39;</span><span class="p">,</span> <span class="n">V</span><span class="p">(</span><span class="s1">&#39;)&#39;</span><span class="p">),</span>
<span class="gp">... </span>        <span class="n">output_field</span><span class="o">=</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="p">)</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">screen_name</span><span class="p">)</span>
<span class="go">Margaret Smith (Maggie)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-left">
<span id="left"></span><h3><code class="docutils literal notranslate"><span class="pre">Left</span></code><a class="headerlink" href="#left" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Left">
<em class="property">class </em><code class="descname">Left</code>(<em>expression</em>, <em>length</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Left" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回给定文本字段或表达式的第一个 <code class="docutils literal notranslate"><span class="pre">length</span></code> 字符。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Left</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">first_initial</span><span class="o">=</span><span class="n">Left</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">first_initial</span><span class="p">)</span>
<span class="go">M</span>
</pre></div>
</div>
</div>
<div class="section" id="s-length">
<span id="length"></span><h3><code class="docutils literal notranslate"><span class="pre">Length</span></code><a class="headerlink" href="#length" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Length">
<em class="property">class </em><code class="descname">Length</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Length" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并返回值的字符数。如果表达式为空，则长度也为空。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get the length of the name and goes_by fields</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Length</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">name_length</span><span class="o">=</span><span class="n">Length</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">),</span>
<span class="gp">... </span>   <span class="n">goes_by_length</span><span class="o">=</span><span class="n">Length</span><span class="p">(</span><span class="s1">&#39;goes_by&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_length</span><span class="p">,</span> <span class="n">author</span><span class="o">.</span><span class="n">goes_by_length</span><span class="p">)</span>
<span class="go">(14, None)</span>
</pre></div>
</div>
<p>也可以注册为变换。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">CharField</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Length</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">CharField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">Length</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Get authors whose name is longer than 7 characters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">authors</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__length__gt</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-lower">
<span id="lower"></span><h3><code class="docutils literal notranslate"><span class="pre">Lower</span></code><a class="headerlink" href="#lower" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Lower">
<em class="property">class </em><code class="descname">Lower</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Lower" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并返回小写表示。</p>
<p>它也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样，作为一个变换注册。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Lower</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">name_lower</span><span class="o">=</span><span class="n">Lower</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_lower</span><span class="p">)</span>
<span class="go">margaret smith</span>
</pre></div>
</div>
</div>
<div class="section" id="s-lpad">
<span id="lpad"></span><h3><code class="docutils literal notranslate"><span class="pre">LPad</span></code><a class="headerlink" href="#lpad" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.LPad">
<em class="property">class </em><code class="descname">LPad</code>(<em>expression</em>, <em>length</em>, <em>fill_text=Value(' ')</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.LPad" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回给定的文本字段或表达式的值，在左侧用 <code class="docutils literal notranslate"><span class="pre">fill_text</span></code> 填充，使结果是 <code class="docutils literal notranslate"><span class="pre">length</span></code> 字符长。默认的 <code class="docutils literal notranslate"><span class="pre">fill_text</span></code> 是一个空格。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Value</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">LPad</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;John&#39;</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">LPad</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">)))</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">abcaJohn</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ltrim">
<span id="ltrim"></span><h3><code class="docutils literal notranslate"><span class="pre">LTrim</span></code><a class="headerlink" href="#ltrim" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.LTrim">
<em class="property">class </em><code class="descname">LTrim</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.LTrim" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>类似于 <code class="xref py py-class docutils literal notranslate"><span class="pre">Trim</span></code>，但只删除前导空格。</p>
</div>
<div class="section" id="s-md5">
<span id="md5"></span><h3><code class="docutils literal notranslate"><span class="pre">MD5</span></code><a class="headerlink" href="#md5" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.MD5">
<em class="property">class </em><code class="descname">MD5</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.MD5" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并返回字符串的 MD5 哈希值。</p>
<p>它也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样，作为一个变换注册。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">MD5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">name_md5</span><span class="o">=</span><span class="n">MD5</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_md5</span><span class="p">)</span>
<span class="go">749fb689816b2db85f5b169c2055b247</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ord">
<span id="ord"></span><h3><code class="docutils literal notranslate"><span class="pre">Ord</span></code><a class="headerlink" href="#ord" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Ord">
<em class="property">class </em><code class="descname">Ord</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Ord" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受一个文本字段或表达式，并返回该表达式第一个字符的 Unicode 码点值。它的工作原理类似于 Python 的 <a class="reference external" href="https://docs.python.org/3/library/functions.html#ord" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">ord()</span></code></a> 函数，但如果表达式超过一个字符，则不会引发异常。</p>
<p>也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样，把它注册为一个变换。默认的查找名称是 <code class="docutils literal notranslate"><span class="pre">ord</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Ord</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">name_code_point</span><span class="o">=</span><span class="n">Ord</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_code_point</span><span class="p">)</span>
<span class="go">77</span>
</pre></div>
</div>
</div>
<div class="section" id="s-repeat">
<span id="repeat"></span><h3><code class="docutils literal notranslate"><span class="pre">Repeat</span></code><a class="headerlink" href="#repeat" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Repeat">
<em class="property">class </em><code class="descname">Repeat</code>(<em>expression</em>, <em>number</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Repeat" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回给定文本字段或表达式重复 <code class="docutils literal notranslate"><span class="pre">number</span></code> 次数的值。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Repeat</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;John&#39;</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">Repeat</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">JohnJohnJohn</span>
</pre></div>
</div>
</div>
<div class="section" id="s-replace">
<span id="replace"></span><h3><code class="docutils literal notranslate"><span class="pre">Replace</span></code><a class="headerlink" href="#replace" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Replace">
<em class="property">class </em><code class="descname">Replace</code>(<em>expression</em>, <em>text</em>, <em>replacement=Value('')</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Replace" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>在 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 中用 <code class="docutils literal notranslate"><span class="pre">replacement</span></code> 替换所有出现的 <code class="docutils literal notranslate"><span class="pre">text</span></code>。默认替换文本是空字符串。函数的参数是区分大小写的。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Value</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Replace</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Johnson&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">Replace</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39;Margaret&#39;</span><span class="p">),</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39;Margareth&#39;</span><span class="p">)))</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)</span>
<span class="go">&lt;QuerySet [{&#39;name&#39;: &#39;Margareth Johnson&#39;}, {&#39;name&#39;: &#39;Margareth Smith&#39;}]&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-reverse">
<span id="reverse"></span><h3><code class="docutils literal notranslate"><span class="pre">Reverse</span></code><a class="headerlink" href="#reverse" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Reverse">
<em class="property">class </em><code class="descname">Reverse</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Reverse" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并将该表达式的字符按相反顺序返回。</p>
<p>也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样，把它注册为一个变换。默认的查询名称是 <code class="docutils literal notranslate"><span class="pre">reverse</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Reverse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">backward</span><span class="o">=</span><span class="n">Reverse</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">backward</span><span class="p">)</span>
<span class="go">htimS teragraM</span>
</pre></div>
</div>
</div>
<div class="section" id="s-right">
<span id="right"></span><h3><code class="docutils literal notranslate"><span class="pre">Right</span></code><a class="headerlink" href="#right" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Right">
<em class="property">class </em><code class="descname">Right</code>(<em>expression</em>, <em>length</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Right" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回给定文本字段或表达式的最后 <code class="docutils literal notranslate"><span class="pre">length</span></code> 字符。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Right</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">last_letter</span><span class="o">=</span><span class="n">Right</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">last_letter</span><span class="p">)</span>
<span class="go">h</span>
</pre></div>
</div>
</div>
<div class="section" id="s-rpad">
<span id="rpad"></span><h3><code class="docutils literal notranslate"><span class="pre">RPad</span></code><a class="headerlink" href="#rpad" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.RPad">
<em class="property">class </em><code class="descname">RPad</code>(<em>expression</em>, <em>length</em>, <em>fill_text=Value(' ')</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.RPad" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>类似于 <code class="xref py py-class docutils literal notranslate"><span class="pre">LPad</span></code>，但垫在右边。</p>
</div>
<div class="section" id="s-rtrim">
<span id="rtrim"></span><h3><code class="docutils literal notranslate"><span class="pre">RTrim</span></code><a class="headerlink" href="#rtrim" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.RTrim">
<em class="property">class </em><code class="descname">RTrim</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.RTrim" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>类似于 <code class="xref py py-class docutils literal notranslate"><span class="pre">Trim</span></code>，但只删除尾部的空格。</p>
</div>
<div class="section" id="s-sha1-sha224-sha256-sha384-and-sha512">
<span id="sha1-sha224-sha256-sha384-and-sha512"></span><h3><code class="docutils literal notranslate"><span class="pre">SHA1</span></code>、<cite>SHA224`</cite>、<cite>SHA256`</cite>、<cite>SHA384`</cite> 和 <code class="docutils literal notranslate"><span class="pre">SHA512</span></code>。<a class="headerlink" href="#sha1-sha224-sha256-sha384-and-sha512" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.SHA1">
<em class="property">class </em><code class="descname">SHA1</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.SHA1" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.db.models.functions.SHA224">
<em class="property">class </em><code class="descname">SHA224</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.SHA224" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.db.models.functions.SHA256">
<em class="property">class </em><code class="descname">SHA256</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.SHA256" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.db.models.functions.SHA384">
<em class="property">class </em><code class="descname">SHA384</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.SHA384" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.db.models.functions.SHA512">
<em class="property">class </em><code class="descname">SHA512</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.SHA512" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并返回字符串的特定哈希值。</p>
<p>它们也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样注册为变换。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">SHA1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">name_sha1</span><span class="o">=</span><span class="n">SHA1</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_sha1</span><span class="p">)</span>
<span class="go">b87efd8a6c991c390be5a68e8a7945a7851c7e5c</span>
</pre></div>
</div>
<div class="admonition-postgresql admonition">
<p class="first admonition-title">PostgreSQL</p>
<p class="last">必须安装 <a class="reference external" href="https://www.postgresql.org/docs/current/pgcrypto.html">pgcrypto 扩展</a> 。你可以使用 <a class="reference internal" href="../contrib/postgres/operations.html#django.contrib.postgres.operations.CryptoExtension" title="django.contrib.postgres.operations.CryptoExtension"><code class="xref py py-class docutils literal notranslate"><span class="pre">CryptoExtension</span></code></a> 迁移操作来安装它。</p>
</div>
<div class="admonition-oracle admonition">
<p class="first admonition-title">Oracle</p>
<p class="last">Oracle 不支持 <code class="docutils literal notranslate"><span class="pre">SHA224</span></code> 函数。</p>
</div>
</div>
<div class="section" id="s-strindex">
<span id="strindex"></span><h3><code class="docutils literal notranslate"><span class="pre">StrIndex</span></code><a class="headerlink" href="#strindex" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.StrIndex">
<em class="property">class </em><code class="descname">StrIndex</code>(<em>string</em>, <em>substring</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.StrIndex" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回一个正整数，对应于 <code class="docutils literal notranslate"><span class="pre">string</span></code> 中第一次出现的 <code class="docutils literal notranslate"><span class="pre">substring</span></code> 的 1 个索引位置，如果没有找到 <code class="docutils literal notranslate"><span class="pre">substring</span></code>，则返回 0。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Value</span> <span class="k">as</span> <span class="n">V</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">StrIndex</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Smith, Margaret&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Jackson&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Jackson&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">smith_index</span><span class="o">=</span><span class="n">StrIndex</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">V</span><span class="p">(</span><span class="s1">&#39;Smith&#39;</span><span class="p">))</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span><span class="o">.</span><span class="n">smith_index</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">authors</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">smith_index</span><span class="o">=</span><span class="n">StrIndex</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">V</span><span class="p">(</span><span class="s1">&#39;Smith&#39;</span><span class="p">))</span>
<span class="gp">... </span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">smith_index__gt</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">&lt;QuerySet [&lt;Author: Margaret Smith&gt;, &lt;Author: Smith, Margaret&gt;]&gt;</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">在 MySQL 中，数据库表的 <a class="reference internal" href="../databases.html#mysql-collation"><span class="std std-ref">字符序</span></a> 决定了字符串比较（如本函数的 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 和 <code class="docutils literal notranslate"><span class="pre">substring</span></code>）是否区分大小写。默认情况下，比较是不区分大小写的。</p>
</div>
</div>
<div class="section" id="s-substr">
<span id="substr"></span><h3><code class="docutils literal notranslate"><span class="pre">Substr</span></code><a class="headerlink" href="#substr" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Substr">
<em class="property">class </em><code class="descname">Substr</code>(<em>expression</em>, <em>pos</em>, <em>length=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Substr" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>从字段或表达式的位置 <code class="docutils literal notranslate"><span class="pre">pos</span></code> 开始返回一个长度为 <code class="docutils literal notranslate"><span class="pre">length</span></code> 的子串。如果 <code class="docutils literal notranslate"><span class="pre">length</span></code> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，那么将返回字符串的其余部分。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Set the alias to the first 5 characters of the name as lowercase</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Lower</span><span class="p">,</span> <span class="n">Substr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">alias</span><span class="o">=</span><span class="n">Lower</span><span class="p">(</span><span class="n">Substr</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">)))</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">alias</span><span class="p">)</span>
<span class="go">marga</span>
</pre></div>
</div>
</div>
<div class="section" id="s-trim">
<span id="trim"></span><h3><code class="docutils literal notranslate"><span class="pre">Trim</span></code><a class="headerlink" href="#trim" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Trim">
<em class="property">class </em><code class="descname">Trim</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Trim" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回给定的文本字段或表达式的值，并去除前导和尾部的空格。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Trim</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;  John  &#39;</span><span class="p">,</span> <span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">Trim</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">alias</span><span class="o">=</span><span class="s1">&#39;j&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">John</span>
</pre></div>
</div>
</div>
<div class="section" id="s-upper">
<span id="upper"></span><h3><code class="docutils literal notranslate"><span class="pre">Upper</span></code><a class="headerlink" href="#upper" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Upper">
<em class="property">class </em><code class="descname">Upper</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Upper" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>接受单个文本字段或表达式，并返回大写表示。</p>
<p>它也可以像 <a class="reference internal" href="#django.db.models.functions.Length" title="django.db.models.functions.Length"><code class="xref py py-class docutils literal notranslate"><span class="pre">Length</span></code></a> 中描述的那样，作为一个变换注册。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Upper</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Margaret Smith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="n">name_upper</span><span class="o">=</span><span class="n">Upper</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">name_upper</span><span class="p">)</span>
<span class="go">MARGARET SMITH</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-window-functions">
<span id="s-id4"></span><span id="window-functions"></span><span id="id4"></span><h2>窗口函数<a class="headerlink" href="#window-functions" title="永久链接至标题">¶</a></h2>
<p>在 <a class="reference internal" href="expressions.html#django.db.models.expressions.Window" title="django.db.models.expressions.Window"><code class="xref py py-class docutils literal notranslate"><span class="pre">Window</span></code></a> 表达式中，有很多函数可以用来计算元素的等级或某些行的 <a class="reference internal" href="#django.db.models.functions.Ntile" title="django.db.models.functions.Ntile"><code class="xref py py-class docutils literal notranslate"><span class="pre">Ntile</span></code></a>。</p>
<div class="section" id="s-cumedist">
<span id="cumedist"></span><h3><code class="docutils literal notranslate"><span class="pre">CumeDist</span></code><a class="headerlink" href="#cumedist" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.CumeDist">
<em class="property">class </em><code class="descname">CumeDist</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.CumeDist" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>计算一个窗口或分区内的数值的累积分布。累计分布被定义为当前行之前的行数或同行行数除以框架中的总行数。</p>
</div>
<div class="section" id="s-denserank">
<span id="denserank"></span><h3><code class="docutils literal notranslate"><span class="pre">DenseRank</span></code><a class="headerlink" href="#denserank" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.DenseRank">
<em class="property">class </em><code class="descname">DenseRank</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.DenseRank" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>相当于 <a class="reference internal" href="#django.db.models.functions.Rank" title="django.db.models.functions.Rank"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rank</span></code></a>，但没有间隙。</p>
</div>
<div class="section" id="s-firstvalue">
<span id="firstvalue"></span><h3><code class="docutils literal notranslate"><span class="pre">FirstValue</span></code><a class="headerlink" href="#firstvalue" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.FirstValue">
<em class="property">class </em><code class="descname">FirstValue</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.FirstValue" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>返回窗口帧第一行的值，如果没有这个值，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</div>
<div class="section" id="s-lag">
<span id="lag"></span><h3><code class="docutils literal notranslate"><span class="pre">Lag</span></code><a class="headerlink" href="#lag" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Lag">
<em class="property">class </em><code class="descname">Lag</code>(<em>expression</em>, <em>offset=1</em>, <em>default=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Lag" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>计算 <code class="docutils literal notranslate"><span class="pre">offset</span></code> 的偏移值，如果没有行存在，返回 <code class="docutils literal notranslate"><span class="pre">default</span></code>。</p>
<p><code class="docutils literal notranslate"><span class="pre">default</span></code> 必须与 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 具有相同的类型，但是，这只由数据库验证，而不是在 Python 中验证。</p>
<div class="admonition-mariadb-and-default admonition">
<p class="first admonition-title">MariaDB 和 <code class="docutils literal notranslate"><span class="pre">default</span></code></p>
<p class="last">MariaDB <a class="reference external" href="https://jira.mariadb.org/browse/MDEV-12981">不支持</a> 这个 <code class="docutils literal notranslate"><span class="pre">default</span></code> 参数。</p>
</div>
</div>
<div class="section" id="s-lastvalue">
<span id="lastvalue"></span><h3><code class="docutils literal notranslate"><span class="pre">LastValue</span></code><a class="headerlink" href="#lastvalue" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.LastValue">
<em class="property">class </em><code class="descname">LastValue</code>(<em>expression</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.LastValue" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>类似于 <a class="reference internal" href="#django.db.models.functions.FirstValue" title="django.db.models.functions.FirstValue"><code class="xref py py-class docutils literal notranslate"><span class="pre">FirstValue</span></code></a>，它计算给定框架子句中的最后一个值。</p>
</div>
<div class="section" id="s-lead">
<span id="lead"></span><h3><code class="docutils literal notranslate"><span class="pre">Lead</span></code><a class="headerlink" href="#lead" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Lead">
<em class="property">class </em><code class="descname">Lead</code>(<em>expression</em>, <em>offset=1</em>, <em>default=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Lead" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>计算给定 <a class="reference internal" href="expressions.html#window-frames"><span class="std std-ref">frame</span></a> 中的前导值。<code class="docutils literal notranslate"><span class="pre">offset</span></code> 和 <code class="docutils literal notranslate"><span class="pre">default</span></code> 都是根据当前行的情况来计算的。</p>
<p><code class="docutils literal notranslate"><span class="pre">default</span></code> 必须与 <code class="docutils literal notranslate"><span class="pre">expression</span></code> 具有相同的类型，但是，这只由数据库验证，而不是在 Python 中验证。</p>
<div class="admonition-mariadb-and-default admonition">
<p class="first admonition-title">MariaDB 和 <code class="docutils literal notranslate"><span class="pre">default</span></code></p>
<p class="last">MariaDB <a class="reference external" href="https://jira.mariadb.org/browse/MDEV-12981">不支持</a> 这个 <code class="docutils literal notranslate"><span class="pre">default</span></code> 参数。</p>
</div>
</div>
<div class="section" id="s-nthvalue">
<span id="nthvalue"></span><h3><code class="docutils literal notranslate"><span class="pre">NthValue</span></code><a class="headerlink" href="#nthvalue" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.NthValue">
<em class="property">class </em><code class="descname">NthValue</code>(<em>expression</em>, <em>nth=1</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.NthValue" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>计算相对于窗口内偏移量 <code class="docutils literal notranslate"><span class="pre">nth</span></code> （必须是正值）的行。如果没有行，返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<p>一些数据库可能会以不同的方式处理不存在的 nth-value，例如，对于基于字符的表达式，Oracle 会返回一个空字符串，而不是 <code class="docutils literal notranslate"><span class="pre">None</span></code>。在这些情况下，Django 不做任何转换。</p>
</div>
<div class="section" id="s-ntile">
<span id="ntile"></span><h3><code class="docutils literal notranslate"><span class="pre">Ntile</span></code><a class="headerlink" href="#ntile" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Ntile">
<em class="property">class </em><code class="descname">Ntile</code>(<em>num_buckets=1</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Ntile" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>为帧子句中的每一行计算一个分区，在 1 和 <code class="docutils literal notranslate"><span class="pre">num_buckets</span></code> 之间尽可能均匀地分配数字。如果行没有被平均分配到若干个桶中，则一个或多个桶将被更频繁地表示。</p>
</div>
<div class="section" id="s-percentrank">
<span id="percentrank"></span><h3><code class="docutils literal notranslate"><span class="pre">PercentRank</span></code><a class="headerlink" href="#percentrank" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.PercentRank">
<em class="property">class </em><code class="descname">PercentRank</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.PercentRank" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>计算帧子句中行的百分位数。这个计算相当于执行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">rank</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">total</span> <span class="n">rows</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>下表解释了行的百分位数的计算：</p>
<table class="docutils">
<colgroup>
<col width="13%" />
<col width="13%" />
<col width="11%" />
<col width="32%" />
<col width="32%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">行 #</th>
<th class="head">值</th>
<th class="head">排名</th>
<th class="head">计算</th>
<th class="head">百分比排名</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>1</td>
<td>15</td>
<td>1</td>
<td>(1-1)/(7-1)</td>
<td>0.0000</td>
</tr>
<tr class="row-odd"><td>2</td>
<td>20</td>
<td>2</td>
<td>(2-1)/(7-1)</td>
<td>0.1666</td>
</tr>
<tr class="row-even"><td>3</td>
<td>20</td>
<td>2</td>
<td>(2-1)/(7-1)</td>
<td>0.1666</td>
</tr>
<tr class="row-odd"><td>4</td>
<td>20</td>
<td>2</td>
<td>(2-1)/(7-1)</td>
<td>0.1666</td>
</tr>
<tr class="row-even"><td>5</td>
<td>30</td>
<td>5</td>
<td>(5-1)/(7-1)</td>
<td>0.6666</td>
</tr>
<tr class="row-odd"><td>6</td>
<td>30</td>
<td>5</td>
<td>(5-1)/(7-1)</td>
<td>0.6666</td>
</tr>
<tr class="row-even"><td>7</td>
<td>40</td>
<td>7</td>
<td>(7-1)/(7-1)</td>
<td>1.0000</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-rank">
<span id="rank"></span><h3><code class="docutils literal notranslate"><span class="pre">Rank</span></code><a class="headerlink" href="#rank" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.Rank">
<em class="property">class </em><code class="descname">Rank</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.Rank" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>与 <code class="docutils literal notranslate"><span class="pre">RowNumber</span></code> 类似，该函数对窗口中的行进行排序。计算出的排名包含有空隙。使用 <a class="reference internal" href="#django.db.models.functions.DenseRank" title="django.db.models.functions.DenseRank"><code class="xref py py-class docutils literal notranslate"><span class="pre">DenseRank</span></code></a> 来计算没有空隙的排名。</p>
</div>
<div class="section" id="s-rownumber">
<span id="rownumber"></span><h3><code class="docutils literal notranslate"><span class="pre">RowNumber</span></code><a class="headerlink" href="#rownumber" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.db.models.functions.RowNumber">
<em class="property">class </em><code class="descname">RowNumber</code>(<em>*expressions</em>, <em>**extra</em>)<a class="headerlink" href="#django.db.models.functions.RowNumber" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>如果没有对 <a class="reference internal" href="expressions.html#window-frames"><span class="std std-ref">窗口帧</span></a> 进行分区，则根据帧子句的顺序或整个查询的顺序计算行数。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">数据库函数</a><ul>
<li><a class="reference internal" href="#comparison-and-conversion-functions">比较和转换函数</a><ul>
<li><a class="reference internal" href="#cast"><code class="docutils literal notranslate"><span class="pre">Cast</span></code></a></li>
<li><a class="reference internal" href="#coalesce"><code class="docutils literal notranslate"><span class="pre">Coalesce</span></code></a></li>
<li><a class="reference internal" href="#collate"><code class="docutils literal notranslate"><span class="pre">Collate</span></code></a></li>
<li><a class="reference internal" href="#greatest"><code class="docutils literal notranslate"><span class="pre">Greatest</span></code></a></li>
<li><a class="reference internal" href="#jsonobject"><code class="docutils literal notranslate"><span class="pre">JSONObject</span></code></a></li>
<li><a class="reference internal" href="#least"><code class="docutils literal notranslate"><span class="pre">Least</span></code></a></li>
<li><a class="reference internal" href="#nullif"><code class="docutils literal notranslate"><span class="pre">NullIf</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#date-functions">日期函数</a><ul>
<li><a class="reference internal" href="#extract"><code class="docutils literal notranslate"><span class="pre">Extract</span></code></a><ul>
<li><a class="reference internal" href="#datefield-extracts"><code class="docutils literal notranslate"><span class="pre">DateField</span></code> 提取</a></li>
<li><a class="reference internal" href="#datetimefield-extracts"><code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 提取</a></li>
</ul>
</li>
<li><a class="reference internal" href="#now"><code class="docutils literal notranslate"><span class="pre">Now</span></code></a></li>
<li><a class="reference internal" href="#trunc"><code class="docutils literal notranslate"><span class="pre">Trunc</span></code></a><ul>
<li><a class="reference internal" href="#datefield-truncation"><code class="docutils literal notranslate"><span class="pre">DateField</span></code> 截断</a></li>
<li><a class="reference internal" href="#datetimefield-truncation"><code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 截断</a></li>
<li><a class="reference internal" href="#timefield-truncation"><code class="docutils literal notranslate"><span class="pre">TimeField</span></code> 截断</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#math-functions">数学函数</a><ul>
<li><a class="reference internal" href="#abs"><code class="docutils literal notranslate"><span class="pre">Abs</span></code></a></li>
<li><a class="reference internal" href="#acos"><code class="docutils literal notranslate"><span class="pre">ACos</span></code></a></li>
<li><a class="reference internal" href="#asin"><code class="docutils literal notranslate"><span class="pre">ASin</span></code></a></li>
<li><a class="reference internal" href="#atan"><code class="docutils literal notranslate"><span class="pre">ATan</span></code></a></li>
<li><a class="reference internal" href="#atan2"><code class="docutils literal notranslate"><span class="pre">ATan2</span></code></a></li>
<li><a class="reference internal" href="#ceil"><code class="docutils literal notranslate"><span class="pre">Ceil</span></code></a></li>
<li><a class="reference internal" href="#cos"><code class="docutils literal notranslate"><span class="pre">Cos</span></code></a></li>
<li><a class="reference internal" href="#cot"><code class="docutils literal notranslate"><span class="pre">Cot</span></code></a></li>
<li><a class="reference internal" href="#degrees"><code class="docutils literal notranslate"><span class="pre">Degrees</span></code></a></li>
<li><a class="reference internal" href="#exp"><code class="docutils literal notranslate"><span class="pre">Exp</span></code></a></li>
<li><a class="reference internal" href="#floor"><code class="docutils literal notranslate"><span class="pre">Floor</span></code></a></li>
<li><a class="reference internal" href="#ln"><code class="docutils literal notranslate"><span class="pre">Ln</span></code></a></li>
<li><a class="reference internal" href="#log"><code class="docutils literal notranslate"><span class="pre">Log</span></code></a></li>
<li><a class="reference internal" href="#mod"><code class="docutils literal notranslate"><span class="pre">Mod</span></code></a></li>
<li><a class="reference internal" href="#pi"><code class="docutils literal notranslate"><span class="pre">Pi</span></code></a></li>
<li><a class="reference internal" href="#power"><code class="docutils literal notranslate"><span class="pre">Power</span></code></a></li>
<li><a class="reference internal" href="#radians"><code class="docutils literal notranslate"><span class="pre">Radians</span></code></a></li>
<li><a class="reference internal" href="#random"><code class="docutils literal notranslate"><span class="pre">Random</span></code></a></li>
<li><a class="reference internal" href="#round"><code class="docutils literal notranslate"><span class="pre">Round</span></code></a></li>
<li><a class="reference internal" href="#sign"><code class="docutils literal notranslate"><span class="pre">Sign</span></code></a></li>
<li><a class="reference internal" href="#sin"><code class="docutils literal notranslate"><span class="pre">Sin</span></code></a></li>
<li><a class="reference internal" href="#sqrt"><code class="docutils literal notranslate"><span class="pre">Sqrt</span></code></a></li>
<li><a class="reference internal" href="#tan"><code class="docutils literal notranslate"><span class="pre">Tan</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#text-functions">文本函数</a><ul>
<li><a class="reference internal" href="#chr"><code class="docutils literal notranslate"><span class="pre">Chr</span></code></a></li>
<li><a class="reference internal" href="#concat"><code class="docutils literal notranslate"><span class="pre">Concat</span></code></a></li>
<li><a class="reference internal" href="#left"><code class="docutils literal notranslate"><span class="pre">Left</span></code></a></li>
<li><a class="reference internal" href="#length"><code class="docutils literal notranslate"><span class="pre">Length</span></code></a></li>
<li><a class="reference internal" href="#lower"><code class="docutils literal notranslate"><span class="pre">Lower</span></code></a></li>
<li><a class="reference internal" href="#lpad"><code class="docutils literal notranslate"><span class="pre">LPad</span></code></a></li>
<li><a class="reference internal" href="#ltrim"><code class="docutils literal notranslate"><span class="pre">LTrim</span></code></a></li>
<li><a class="reference internal" href="#md5"><code class="docutils literal notranslate"><span class="pre">MD5</span></code></a></li>
<li><a class="reference internal" href="#ord"><code class="docutils literal notranslate"><span class="pre">Ord</span></code></a></li>
<li><a class="reference internal" href="#repeat"><code class="docutils literal notranslate"><span class="pre">Repeat</span></code></a></li>
<li><a class="reference internal" href="#replace"><code class="docutils literal notranslate"><span class="pre">Replace</span></code></a></li>
<li><a class="reference internal" href="#reverse"><code class="docutils literal notranslate"><span class="pre">Reverse</span></code></a></li>
<li><a class="reference internal" href="#right"><code class="docutils literal notranslate"><span class="pre">Right</span></code></a></li>
<li><a class="reference internal" href="#rpad"><code class="docutils literal notranslate"><span class="pre">RPad</span></code></a></li>
<li><a class="reference internal" href="#rtrim"><code class="docutils literal notranslate"><span class="pre">RTrim</span></code></a></li>
<li><a class="reference internal" href="#sha1-sha224-sha256-sha384-and-sha512"><code class="docutils literal notranslate"><span class="pre">SHA1</span></code>、<cite>SHA224`</cite>、<cite>SHA256`</cite>、<cite>SHA384`</cite> 和 <code class="docutils literal notranslate"><span class="pre">SHA512</span></code>。</a></li>
<li><a class="reference internal" href="#strindex"><code class="docutils literal notranslate"><span class="pre">StrIndex</span></code></a></li>
<li><a class="reference internal" href="#substr"><code class="docutils literal notranslate"><span class="pre">Substr</span></code></a></li>
<li><a class="reference internal" href="#trim"><code class="docutils literal notranslate"><span class="pre">Trim</span></code></a></li>
<li><a class="reference internal" href="#upper"><code class="docutils literal notranslate"><span class="pre">Upper</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#window-functions">窗口函数</a><ul>
<li><a class="reference internal" href="#cumedist"><code class="docutils literal notranslate"><span class="pre">CumeDist</span></code></a></li>
<li><a class="reference internal" href="#denserank"><code class="docutils literal notranslate"><span class="pre">DenseRank</span></code></a></li>
<li><a class="reference internal" href="#firstvalue"><code class="docutils literal notranslate"><span class="pre">FirstValue</span></code></a></li>
<li><a class="reference internal" href="#lag"><code class="docutils literal notranslate"><span class="pre">Lag</span></code></a></li>
<li><a class="reference internal" href="#lastvalue"><code class="docutils literal notranslate"><span class="pre">LastValue</span></code></a></li>
<li><a class="reference internal" href="#lead"><code class="docutils literal notranslate"><span class="pre">Lead</span></code></a></li>
<li><a class="reference internal" href="#nthvalue"><code class="docutils literal notranslate"><span class="pre">NthValue</span></code></a></li>
<li><a class="reference internal" href="#ntile"><code class="docutils literal notranslate"><span class="pre">Ntile</span></code></a></li>
<li><a class="reference internal" href="#percentrank"><code class="docutils literal notranslate"><span class="pre">PercentRank</span></code></a></li>
<li><a class="reference internal" href="#rank"><code class="docutils literal notranslate"><span class="pre">Rank</span></code></a></li>
<li><a class="reference internal" href="#rownumber"><code class="docutils literal notranslate"><span class="pre">RowNumber</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="conditional-expressions.html"
                        title="上一章">条件表达式</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="../paginator.html"
                        title="下一章">分页器</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/database-functions.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="conditional-expressions.html" title="条件表达式">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="../paginator.html" title="分页器">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>